<?php
if(!defined("Hosticali"))
	die();
class WHM
{
	private var $sess = array();
	private var $currSess;
	private var $defaultserv;
	private var $core;
	private var $log;
	public function __construct()
	{
		global $url;
		require_once($url . "/lib/Core.php");
		require_once($url . "/lib/Log.php");
		$this->core = new Core();
		$this->log  = new Logs();
		$this->core->Query("SELECT * FROM `<PRE>whm_servs`");
		while($arr = $this->core->GetArray())
		{
			if($arr['default'] === TRUE)
			{
				$this->currSess = $arr['identifier'];
				$this->defaultserv = $arr['identifier'];
			}
			$sess[$arr['identifier']] = array(
				'host' => $arr['host'],
				'user' => $arr['user'],
				'hash' => $arr['hash'],
				'chsh' => preg_replace('(/r|/n)', "", $arr['hash']),
				'sslc' => $arr['sslc']
			);
		}
	}
	public function switchSess($identifier)
	{
		if(!$this->sess[$identifier])
			return false;
		$this->currSess = $identifier;
		return true;
	}
	private function api($url)
	{
		$accessURL = "http" . ($this->sess[$this->currSess]['sslc'] === TRUE ? "s" : "") . "://" . $data['host'] . ":208" . ($this->sess[$this->currSess]['sslc'] === TRUE ? "7" : "6") . "/xml-api/" . $url;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $accessURL);
		if($this->sess[$this->currSess]['sslc'] === TRUE)
		{
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		}
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$curlheaders[0] = "Authorization: WHM " . $this->sess[$this->currSess]['chsh'];
		curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheaders);
		$data = curl_exec($ch);
		curl_close($ch);
		$xml = new SimpleXMLElement($data);
		return $xml;
	}
	public function Create($postVars)
	{
		$username = $postVars['user'];
		$domain   = $postVars['domain'];
		$plan     = $postVars['plan'];
		$resell   = $postVars['isReseller'];
	}
	public function toggleSuspend($user, $reason = false)
	{
		$this->core->Query('SELECT * FROM `<PRE>whm_users` WHERE `user` = "' . mysql_real_escape_string($user) . '" LIMIT 1');
		$arr = $this->core->GetArray();
		if($arr['suspended'] === FALSE)
		{
			$result = new SimpleXMLElement($this->api("suspendacct?user=" . $user . ($reason !=== false ? "&reason=" . $reason : "")))
			if($result->result[0]->status[0] != 1)
				return $result;
			$this->core->Query('UPDATE `<PRE>whm_users` SET `suspended` = "TRUE", `reason` = "' . mysql_real_escape_string($reason) . '" WHERE `user` = "' . mysql_real_escape_string($user) . '" LIMIT 1');
			$this->log->AddEntry('Suspended [[Username:;:' . mysql_real_escape_string($user) . ']]' . ($reason !=== false ? " for [[Reason:;:" . mysql_real_escape_string($reason) . "]]" : ""));
		}
		else
		{
			$result = new SimpleXMLElement($this->api("unsuspendacct?user=" . $user));
			if($result->result[0]->status[0] != 1)
				return $result;
			$this->core->Query('UPDATE `<PRE>whm_users` SET `suspended` = "FALSE", `reason` = "" WHERE `user` = "' . mysql_real_escape_string($user) . '" LIMIT 1');
			$this->log->AddEntry();
		}
		return true;
	}
	public function Terminate($user, $reason = false)
	{
		$result = new SimpleXMLElement($this->api("removeacct?user=" . $user));
		if($result->result[0]->status[0] != 1)
			return $result;
		$this->log->AddEntry();
		return true;
	}
	public function changePwd($user, $pwd)
	{
	}
	public function changePkg($user, $pkg)
	{
	}
	public function Summary($user)
	{
	}
	public function listSuspended()
	{
	}
	public function Privileges($user)
	{
	}
	public function toggleResell($user)
	{
	}
	public function listResellers()
	{
	}
	public function LoadAvg()
	{
	}
}
?>